package 链表;

import java.util.ArrayList;
import java.util.List;
// https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f?tpId=295&tqId=1008769&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
// https://leetcode.cn/problems/palindrome-linked-list/description/?envType=featured-list&envId=2cktkvj?envType=featured-list&envId=2cktkvj
public class BM13_判断一个链表是否为回文结构 {

	public static void main(String[] args) {
		ListNode node1 = new ListNode(1);
		ListNode node2 = new ListNode(2);
		ListNode node3 = new ListNode(2);
		ListNode node4 = new ListNode(1);
		node1.next = node2;
		node2.next = node3;
		node3.next = node4;
		BM13_判断一个链表是否为回文结构 test = new BM13_判断一个链表是否为回文结构();
		System.out.println(test.isPail(node1));

	}

	public boolean isPail (ListNode head) {
        List<ListNode> list = new ArrayList<>();
        while(head != null) {
        	list.add(head);
        	head = head.next;
        }
        int i = 0;
        int j = list.size()-1;
        while(i < j) {
        	if(list.get(i).val != list.get(j).val) {
        		return false;
        	}
        	i++;
        	j--;
        }
        return true;
    }
}
